package com.abcode.userserver.client.service;

import com.abcode.userserver.common.constant.Constant;
import com.abcode.userserver.common.vo.ResultVO;
import com.abcode.userserver.entity.SysUserBean;
import com.abcode.userserver.remote.IEmailServer;
import com.abcode.userserver.repository.SysUserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;

/**
 * @Description:
 * @Author:Andy.Yang
 * @Date:2019/4/25 0025
 */
@Service
@Transactional
public class SysUserService {

  @Autowired
  private IEmailServer emailServer;

  @Autowired
  private SysUserRepository sysUserRepository;
  public ResultVO login(String fusername, String fpassword) {
    SysUserBean sysUserBean= sysUserRepository.findFirstByFusernameAndFpassword(fusername,fpassword);
    if(sysUserBean!=null){
      return new ResultVO("登录成功",sysUserBean);
    }else{
      return new ResultVO(Constant.ReturnStatusCode.ERROR,"用户名或密码错误");
    }
  }

  public boolean saveUser(SysUserBean bean) {
    if(sysUserRepository.existsByFusername(bean.getFusername())){
      return false;
    }else {
      sysUserRepository.save(bean);
      //发送邮件
      emailServer.sendEmail("564493864@qq.com","测试","测试");
      return true;
    }
  }
}
